package com.hamster.crmpre.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hamster.crmpre.mapper.PersonalMoneyMapper;
import com.hamster.crmpre.pojo.PersonalMoney;
import com.hamster.crmpre.service.PersonalMoneyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 个人明细  流水账 Service
 * @author Tarry
 * @create 2020/1/9 16:58
 */
@Service
public class PersonalMoneyServiceImpl implements PersonalMoneyService {
    @Autowired
    private PersonalMoneyMapper personalMoneyMapper;
    /**
     * 查询个人收支明细 流水账
     * @param userId  登录人的id
     * @param curMonthTime    本月
     * @param nextMonthTime   下个月
     * @return
     */
    @Override
    public Map<String, Object> personalDetails(String userId, Date curMonthTime, Date nextMonthTime) {
        Map<String, Object> map = new HashMap<>();
        QueryWrapper<PersonalMoney> personalMoneyQueryWrapper = new QueryWrapper<>();
        personalMoneyQueryWrapper.eq("person_id",userId);
        personalMoneyQueryWrapper.ge("create_time",curMonthTime).lt("create_time",nextMonthTime);
        personalMoneyQueryWrapper.orderByDesc("create_time");
        List<PersonalMoney> personalMonies = personalMoneyMapper.selectList(personalMoneyQueryWrapper);
        Double currentMoney = personalMoneyMapper.queryCountMoney(userId,curMonthTime,nextMonthTime);

        map.put("details",personalMonies);
        map.put("currentMoney",currentMoney);
        //如果没有记录，则总额显示0
        if (personalMonies == null ||personalMonies.size() == 0 ){
            map.put("currentMoney",0);
        }
        return map;
    }

    /**
     * 获取指定月份区间的，业绩总和
     * @param userId
     * @param curMonthTime
     * @param nextMonthTime
     * @return
     */
    @Override
    public Double personalMoneyCount(String userId, Date curMonthTime, Date nextMonthTime) {
        Double currentMoney = personalMoneyMapper.queryCountMoney(userId,curMonthTime,nextMonthTime);
        return currentMoney;
    }


}
